package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.*;
import com.cskaoyan.catmall.model.bo.GoodsBo;
import com.cskaoyan.catmall.model.bo.GoodsDetailBo;
import com.cskaoyan.catmall.model.pojo.*;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * GoodsService的实现类
 * 创建日期: 2022/01/07 20:38
 *
 * @author ghr
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;

    /**
     * @param param
     * @return com.cskaoyan.catmall.model.vo.GoodsVo
     * @description 商品页面
     * @author ghr
     * @date 2022/1/8 11:27 上午
     */
    @Override
    public GoodsVo selectGoodsList(GoodsBo goodsBo) {
        Integer page = goodsBo.getPage();
        Integer limit = goodsBo.getLimit();
        String sort = goodsBo.getSort();
        String order = goodsBo.getOrder();
        Integer goodsId = goodsBo.getGoodsId();
        String goodsSn = goodsBo.getGoodsSn();
        String name = goodsBo.getName();
        if (page == null && sort == null && order == null) {
            page = 0;
            sort = "add_time";
            order = "desc";
            List<MarketGoods> allGoods = marketGoodsMapper.selectAllGoods();
            return GoodsVo.goodsVo(0, 0, 0, 0, allGoods);
        }
        PageHelper.startPage(page, limit);

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        StringBuffer clause = new StringBuffer();
        clause.append(sort).append(" ").append(order);
        marketGoodsExample.setOrderByClause(clause + "");
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (goodsId != null) {
            criteria.andIdEqualTo(goodsId);
        }
        if (goodsSn != null && !goodsSn.isEmpty()) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketGoods> list = marketGoodsMapper.selectByExampleWithBLOBs(marketGoodsExample);

        PageInfo<MarketGoods> marketGoodsPageInfo = new PageInfo<>(list);
        long total = marketGoodsPageInfo.getTotal();
        int pages = marketGoodsPageInfo.getPages();
        return GoodsVo.goodsVo(total, pages, limit, page, list);
    }

    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.CatAndBrandVo
     * @description 商品品牌和类目
     * @author ghr
     * @date 2022/1/9 5:29 下午
     */
    @Override
    public CatAndBrandVo selectCatAndBrand() {
        List<BrandVo> brandList = brandMapper.select();
        List<CategoryVo> categoryList = categoryMapper.select();
        for (CategoryVo categoryVo : categoryList) {
            Integer value = categoryVo.getValue();
            List<CategoryChildVo> categoryChildVos = categoryMapper.selectChild(value);
            categoryVo.setChildren(categoryChildVos);
        }
        CatAndBrandVo catAndBrandVo = new CatAndBrandVo();
        catAndBrandVo.setBrandList(brandList);
        catAndBrandVo.setCategoryList(categoryList);
        return catAndBrandVo;
    }

    /**
     * @param
     * @param id
     * @return com.cskaoyan.catmall.model.vo.GoodsDetailVo
     * @description 查找商品细节
     * @author ghr
     * @date 2022/1/9 3:20 下午
     */
    @Override
    public GoodsDetailVo selectGoodsDetail(Integer id) {
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria1 = marketGoodsAttributeExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);

        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);

        Integer categoryId = marketGoods.getCategoryId();
        Integer pid = categoryMapper.selectCrossCategory(categoryId);
        Integer[] categoryIds = new Integer[]{pid, categoryId};

        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria2 = marketGoodsProductExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);

        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria3 = marketGoodsSpecificationExample.createCriteria();
        criteria3.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);

        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setAttributes(marketGoodsAttributes);
        goodsDetailVo.setCategoryIds(categoryIds);
        goodsDetailVo.setGoods(marketGoods);
        goodsDetailVo.setProducts(marketGoodsProducts);
        goodsDetailVo.setSpecifications(marketGoodsSpecifications);

        return goodsDetailVo;
    }

    /**
     * @param
     * @param goodsDetailBo
     * @return void
     * @description 向数据库插入商品细节
     * @author ghr
     * @date 2022/1/9 3:21 下午
     */
    @Override
    public void insertGoodsDetail(GoodsDetailBo goodsDetailBo) {
        Date date = new Date();
        List<MarketGoodsAttribute> attributes = goodsDetailBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            marketGoodsAttributeMapper.insertSelective(attribute);
        }

        MarketGoods goods = goodsDetailBo.getGoods();
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        marketGoodsMapper.insertSelective(goods);

        List<MarketGoodsProduct> products = goodsDetailBo.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setAddTime(date);
            product.setUpdateTime(date);
            marketGoodsProductMapper.insertSelective(product);
        }

        List<MarketGoodsSpecification> specifications = goodsDetailBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            marketGoodsSpecificationMapper.insertSelective(specification);
        }
    }

    /**
     * @param
     * @param marketGoods
     * @return void
     * @description 删除商品
     * @author ghr
     * @date 2022/1/9 4:02 下午
     */
    @Override
    public void deleteGoods(MarketGoods marketGoods) {
        Integer id = marketGoods.getId();
        marketGoods.setDeleted(true);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(id);
        marketGoodsMapper.updateByExampleSelective(marketGoods, marketGoodsExample);
    }

    /**
     * @param
     * @param goodsDetailBo
     * @return java.lang.Integer
     * @description 编辑商品
     * @author ghr
     * @date 2022/1/9 4:27 下午
     */
    @Override
    public void updateGoods(GoodsDetailBo goodsDetailBo) {
        Date date = new Date();
        MarketGoods goods = goodsDetailBo.getGoods();
        goods.setUpdateTime(date);
        marketGoodsMapper.updateByPrimaryKeySelective(goods);

        List<MarketGoodsAttribute> attributes = goodsDetailBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(date);
            marketGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }

        List<MarketGoodsProduct> products = goodsDetailBo.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setUpdateTime(date);
            marketGoodsProductMapper.updateByPrimaryKeySelective(product);
        }

        List<MarketGoodsSpecification> specifications = goodsDetailBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setUpdateTime(date);
            marketGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }
    }

    /**
     * @param
     * @param goodsSn
     * @param name
     * @return int
     * @description 查找重复商品
     * @author ghr
     * @date 2022/1/10 9:03 上午
     */
    @Override
    public int selectDuplicateGoods(String goodsSn, String name) {
        int num = marketGoodsMapper.selectDuplicateGoods(goodsSn, name);
        return num;
    }

    /**
     * @param
     * @param name
     * @return int
     * @description 通过商品名查重复商品
     * @author ghr
     * @date 2022/1/10 9:26 上午
     */
    @Override
    public int selectDuplicateGoodsByName(String name) {
        int num = marketGoodsMapper.selectDuplicateGoodsByName(name);
        return num;
    }
}